package com.jk.bglog.service;

import com.jk.bglog.dao.UserDao;
import com.jk.bglog.entity.OrderLogEntity;
import com.jk.bglog.entity.UserInfoEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.logging.java.SimpleFormatter;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class UserService {
    @Resource
    private UserDao UserDao;
    @Autowired
    private MongoTemplate MongoTemplate;

//    public void addUserInfo(UserInfoEntity userInfoEntity) {
//        UserInfoEntity a = new UserInfoEntity();
//        a.setUserDate("1");
//        a.setUserId(1);
//        a.setUserName("1");
//        a.setUserNum("1");
//        a.setUserOperate("1");
//        MongoTemplate.insert(a);
//    }

    public List<UserInfoEntity> queryUserInfo(UserInfoEntity userInfoEntity) {
        Query query = new Query();
        if (StringUtils.isNotBlank(userInfoEntity.getUserLogStartTime()) && StringUtils.isNotBlank(userInfoEntity.getUserLogEndTime())){
            query.addCriteria(Criteria.where("userDate").lte(userInfoEntity.getUserLogEndTime()).gte(userInfoEntity.getUserLogStartTime()));
        }if (userInfoEntity.getUserName() != null && userInfoEntity.getUserName() != ""){
            query.addCriteria(Criteria.where("userName").regex(userInfoEntity.getUserName()));
        }if(userInfoEntity.getUserOperate()!= null && userInfoEntity.getUserOperate()!= ""){
            query.addCriteria(Criteria.where("userOperate").is(userInfoEntity.getUserOperate()));
        }

        List<UserInfoEntity> userInfoEntities = MongoTemplate.find(query, UserInfoEntity.class);

        return userInfoEntities;
    }

    public OrderLogEntity queryOrderById(Integer id){
        Query query = new Query();
        Query query2 = new Query();
        query.addCriteria(Criteria.where("userId").is(id));
        query2.addCriteria(Criteria.where("buyUserId").is(id));
        UserInfoEntity user = MongoTemplate.findOne(query, UserInfoEntity.class);
        OrderLogEntity order = MongoTemplate.findOne(query2, OrderLogEntity.class);
        System.out.println("------------"+order);
        if (user.getUserDate().equals(order.getOrderDate())){

            return order;
        }else {

            return null;
        }

    }
}
